package com.digdes.calendar.gwt.client.widget;

import com.digdes.calendar.gwt.client.dto.EventDTOImpl;
import com.digdes.calendar.gwt.client.service.CalendarServiceAsync;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;
import java.util.List;

public abstract class CalendarWidget extends Composite implements ClickListener {
    protected CalendarServiceAsync calendarService = RpcInit.initRpc();

    protected EventsPanel eventsPanel = new EventsPanel();
    protected List<EventLabel> eventLabels = new ArrayList<EventLabel>();
    protected List<EventDTOImpl> eventDTOList = new ArrayList<EventDTOImpl>();
    protected ShowEventDBox eventDBox = ShowEventDBox.Instance();

    protected EventLabel label;
    protected CellGrid gridInCell;
    protected List<CellGrid> gridInCellList = new ArrayList<CellGrid>();

    protected DockPanel mainPanel = new DockPanel();
    protected AbsolutePanel aPanel = new AbsolutePanel();
    protected FlexTable calendarHeader = new FlexTable();
    protected FlexTable calendarGrid = new FlexTable();


    public CalendarWidget() {
        setWidget(mainPanel);
        Initialize();
    }

    protected CellGrid createCellGrid() {
        gridInCell = new CellGrid(2, 1);
        return gridInCell;
    }

    protected EventLabel createLabel(EventDTOImpl dto) {
        label = new EventLabel(dto);
        label.addClickListener(this);
        return label;
    }

    public void onClick(Widget sender) {
        label = (EventLabel) sender;
        eventDBox.draw(label.getEventDTO());
        eventDBox.deleteBtn.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                //deleteEvent(label);
            }
        });
        eventDBox.center();
    }

    public void Initialize() {
        mainPanel.setStyleName("CalendarWidget");
        calendarHeader.setStyleName("weekheader");
        calendarHeader.setCellPadding(0);
        calendarHeader.setCellSpacing(0);
        calendarHeader.setWidth("1011px");

        calendarGrid.setCellPadding(0);
        calendarGrid.setCellSpacing(0);
        calendarGrid.setHeight("550px");

        setSizeAbsolutePanel();

        aPanel.add(calendarGrid);
        mainPanel.add(getNavBar(), DockPanel.NORTH);
        mainPanel.add(calendarHeader, DockPanel.NORTH);
    }

    public EventsPanel getEventsPanel() {
        return eventsPanel;
    }

    protected abstract void drawCalendar();

    //public  abstract void clearCalendar();

    public AbsolutePanel getAbsolutePanel() {
        return aPanel;
    }

    public void setSizeAbsolutePanel() {
        aPanel.setSize("100%", "100%");
    }

    protected abstract Widget getNavBar();

}
